package com.xca.rxjavademo.adapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.squareup.picasso.Picasso;
import com.xca.rxjavademo.R;
import com.xca.rxjavademo.entity.DayImage;
import com.xca.rxjavademo.utils.ImageDownloader;

import java.util.List;

public class DayImgAdapter extends RecyclerView.Adapter<DayImgAdapter.ViewHolder>{
    private List<DayImage.ImageData> mList ;
    private Context context ;
    private LayoutInflater inflater; // 用于加载布局
    private Activity activity ;
    public DayImgAdapter(List<DayImage.ImageData> list , Context context , Activity activity){
        this.mList = list ;
        this.context = context;
        this.activity = activity ;
        this.inflater = LayoutInflater.from(context);
    }

    public void updateData(List<DayImage.ImageData> list){
        mList.clear();
        mList = list ;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item , parent , false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DayImage.ImageData imageData = mList.get(position);
        Picasso.get()
                .load(imageData.getUrl())
                .fit()
                .centerInside()
                .into(holder.imageView);
        holder.textView.setText(imageData.getTitle());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDetailDialog(imageData);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView ;
        TextView textView ;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView = itemView.findViewById(R.id.item_img);
            textView = itemView.findViewById(R.id.item_text);
        }
    }

    private void showDetailDialog(DayImage.ImageData data){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        View dialogView = inflater.inflate(R.layout.day_img_show , null);
        TextView tv1 = dialogView.findViewById(R.id.iTv2);
        TextView tv2 = dialogView.findViewById(R.id.iTv3);
        TextView tv3 = dialogView.findViewById(R.id.iTv4);
        ImageView img = dialogView.findViewById(R.id.iImg);
       tv1.setText(data.getCopyright());
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImageDownloader imageDownloader = new ImageDownloader(context , activity);
                imageDownloader.saveImage(data.getUrl());
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openWebsite(data.getCopyrightlink());
            }
        });
        Picasso.get()
                .load(data.getUrl())
                .fit()
                .centerInside()
                .into(img);
        builder.setView(dialogView)
                .setTitle(data.getTitle())
                .create()
                .show();
    }

    // 打开网页的方法
    private void openWebsite(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        if (intent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(intent);
        } else {
            Toast.makeText(context, "无法打开网页，请检查设备是否安装了浏览器", Toast.LENGTH_SHORT).show();
        }
    }
}
